package ie.timalb.chess.test.movement;

import ie.timalb.chess.main.core.Board;
import ie.timalb.chess.main.core.exceptions.ChessException;
import ie.timalb.chess.main.core.exceptions.ImpossibleToTraceARouteException;
import ie.timalb.chess.main.core.exceptions.NoPieceInSrcSquareException;
import ie.timalb.chess.main.core.exceptions.SquareOutOfRangeException;
import ie.timalb.chess.main.core.movement.Location;
import ie.timalb.chess.main.core.movement.Movement;
import ie.timalb.chess.main.core.pieces.Pawn;
import ie.timalb.chess.main.core.pieces.Piece.Color;
import ie.timalb.chess.main.core.squares.Square;
import ie.timalb.chess.main.core.temp.SquareIterator;
import junit.framework.TestCase;

public class MovementTester extends TestCase {
	public void testKOCreationOfMovementFromEmptySquare ()
	{
		//Creates a movement from a board
		boolean bActual = false;
		Board board = new Board();
		Location srcLocation = new Location (2,2);
		Location dtnyLocation = new Location (5,5);
		try {
			Movement mov = board.createMovement(board.getSquare(srcLocation), board.getSquare(dtnyLocation));
		} catch (NoPieceInSrcSquareException e) {
			bActual = true;
		} catch (ChessException e) {
			bActual = false;
		} 
		
		assertEquals(true, bActual);
	}
	
	public void testKOCreationOfMovementInvalidRoute ()
	{
		//Creates a movement from a board
		boolean bActual = false;
		Board board = new Board();
		Location srcLocation = new Location (2,2);
		Location dtnyLocation = new Location (5,8);
		try {
			board.addPiece(new Pawn(Color.BLACK), srcLocation);
		} catch (SquareOutOfRangeException e1) {
			bActual = false;
		}
		
		try {
			Movement mov = board.createMovement(board.getSquare(srcLocation), board.getSquare(dtnyLocation));
		} catch (ImpossibleToTraceARouteException e) {
			bActual = true;
		} catch (ChessException e) {
			bActual = false;
		} 
		
		assertEquals(true, bActual);
	}
	
	public void testOKCreatingMovement ()
	{
		//Creates a movement from a board
		boolean actual = false;
		Board board = new Board();
		Location srcLocation = new Location (2,2);
		Location dtnyLocation = new Location (5,5);
		try {
			board.addPiece(new Pawn(Color.BLACK), srcLocation);
		} catch (SquareOutOfRangeException e1) {
			actual = false;
		}
		Movement mov=null;
		try {
			mov = board.createMovement(board.getSquare(srcLocation), board.getSquare(dtnyLocation));
			actual = true;
		} catch (ChessException e) {
			actual = false;
		} 
		
		assertEquals(true, actual);
		
		SquareIterator it = mov.getIterator(true, true);
		Square stepSquare;
		int iHelper = 1;
		boolean isRightStep = false;
		Location rightLocation = null;
		
		while (it.hasNext())
		{
			stepSquare = it.next();
			rightLocation = new Location (srcLocation.getXCoordinate() + iHelper - 1, srcLocation.getYCoordinate() + iHelper - 1);
			isRightStep = stepSquare.getLocation().equals(rightLocation); 
			assertEquals(true, isRightStep);
			iHelper ++;
		}
	}
}
